/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: objective.js 8 2012-03-11 14:16:41Z cc.hgfeng@gmail.com $
 */

/**
 * OTR.Objective 面向对象扩展方法
 * 
 * reference to ExtJS & John Resig
 * 
 */

/**
 * 合并参数类表中对象的成员，并返回包含所有成员的对象。
 * 如果存在同一成员名称，靠前传入的参数成员将被覆盖。
 * 
 * @param {Object} arguments[]
 * @returns {Object}
 */
OTR.apply = function() {
    var argc = arguments.length;
    var ret  = arguments[0];

    for (var i = 0; i < argc; i++) {
        var obj = arguments[i];
        if (!OTR.Util.isObject(obj)) {
            continue ;
        }
        
        for (var k in obj) {
            ret[k] = obj[k];
        }
    }
    
    return ret;
};

OTR.apply(OTR, {
    /**
     * 重写指定对象prototype中的方法
     * 
     * @param object oriclass
     * @param object overrides
     */
    override: function(oriclass, overrides) {
        if(overrides){
            var p = oriclass.prototype;
            OTR.apply(p, overrides);
            if(OTR.Client.browser(OTR.Client.MSIE) && overrides.toString != oriclass.toString){
                p.toString = overrides.toString;
            }
        }
    },

    /**
     * 类继承实现方法
     * 
     * var Child = OTR.extend(Parent, {
     *     childMethod: function(){},
     *     overrideMethod: function(){
     *         Child.__super.overrideMethod.call(this);
     *     }
     * });
     * 
     * @param object parent
     * @param object overrides
     * @return object
     */
    extend: function(parent, overrides) {
        if (typeof parent != 'function') {
            //throw 'Invalid Parent class';
        }

        var op = Object.prototype.constructor,
            ch = overrides.constructor != op
               ? overrides.constructor
               : function(){
                   parent.apply(this, arguments);
               },
            pp = parent.prototype,
            F  = function(){};

        F.prototype = pp;

        var props = ch.prototype = new F();
        props.constructor = ch;

        ch.__super = pp;
        if (pp.constructor == op) {
            pp.constructor = parent;
        }
        
        OTR.override(ch, overrides);

        props.__super = (function(){return pp;});

        return ch;
    }
});